<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
    <!-- <link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/mycss.css') }}" /> -->
    <link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/fupan_css.css') }}" />
</head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>涨停板</title>
<script src="{{ url_for('static',filename='js/jquery-2.1.1.min.js') }}"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@5.2.2/dist/echarts.min.js"></script>

<body>

    <!-- <div id="menu" style="background-color:#FFD700;width:10%;float:left;">
        <label>
            <span>选择日期</span>
            <input type="date" id="m_date">
        </label>
        <input type="button" onclick="copy_text()" value="确定" />
        <script type="text/javascript">
            function copy_text() {
                date = document.getElementById("m_date").value;
                console.log(date);
                get_data(date);
            }

        </script>
    </div> -->
    <div id="createtable" style="width:60%;float:left;" align="center">
        <h2 id="zt" style="margin-bottom:0;background-color:#FFA500;width:85%;">涨停板</h2>
        <div class="table-one" align="center">
            <table id="mytable" cellspacing="0">
                <tr class="cur">
                    <td>股票名称</td>
                    <td>涨停时间</td>
                    <td>状态</td>
                    <td>涨停原因</td>
                    <td>板块</td>
                    <td>最后涨停时间</td>
                    <td>最大封单</td>
                    <td>封单</td>
                    <td>主力净额</td>
                    <td>成交额</td>
                    <td>实际换手</td>
                    <td>实际流通</td>
                </tr>
            </table>
        </div>
    </div>
    <div id="container" style="height:400px;width:30%;float:left;margin-top:60px">

    </div>

    <script type="text/javascript">
        var column_table = 12; //总列数
        var all_theme = []; //题材数组
        var ID_THREAD = -1; //循环线程
        // 发送ajax请求，从后台获取json数据
        $(document).ready(function() {
            auto_update();
        });

        function UpdateTable(data) {
            all_theme = [];
            update_tables(data)
            for (var row = 0; row < data['list'].length; row++) {
                var tb = document.getElementById("mytable"); //获取表格的dom节点

                for (var column = 0; column < column_table; column++) {
                    var td = tb.rows[row + 1].cells[column]; //获取0行0列的td单元格
                    //td.setAttribute('class', 'black');       //设置属性

                    if (column == 0) {
                        var code = data.list[row][column]
                        var name = data.list[row][column + 1]
                            // < !--td.innerHTML = "<div class=\"tb1 name\">" + String(name) + "</div>" + "<div class=\"code\">" + String(code) + "</div>"-- >
                            //td.innerHTML = "<a href=\"https://www.baidu.com\">" + String(name) + "</div>"
                        td.innerHTML = name
                        if (data.list[row][24]) {
                            td.setAttribute('class', 'green'); //设置属性
                        }
                    } else if (column == 1) {
                        var date = new Date(parseInt(data.list[row][6]) * 1000)
                        td.innerHTML = format_date(date);
                    } else if (column == 2) {
                        td.innerHTML = data.list[row][9];
                        if (data.list[row][9] != "首板") {
                            td.setAttribute('class', 'red'); //设置属性
                        }
                    } else if (column == 3) {
                        td.innerHTML = data.list[row][16];
                        //数据统计
                        all_theme = all_theme.concat({
                            name: data.list[row][16],
                            value: 1
                        })
                    } else if (column == 4) {
                        td.innerHTML = data.list[row][11];
                    } else if (column == 5) {
                        var date = new Date(parseInt(data.list[row][25]) * 1000)
                        td.innerHTML = format_date(date);
                    } else if (column == 6) {
                        td.innerHTML = format_money(data.list[row][23]);
                    } else if (column == 7) {
                        td.innerHTML = format_money(data.list[row][22]);
                    } else if (column == 8) {
                        td.innerHTML = format_money(data.list[row][12]);
                    } else if (column == 9) {
                        td.innerHTML = format_money(data.list[row][13]);
                    } else if (column == 10) {
                        td.innerHTML = (data.list[row][14] + "%");
                    } else if (column == 11) {
                        td.innerHTML = format_money(data.list[row][15]);
                    } else {
                        td.innerHTML = data.list[row][column]
                    }
                }

            }

        }

        function history_update_table(data) {
            //更新历史涨停
            all_theme = [];
            update_tables(data)
            for (var row = 0; row < data['list'].length; row++) {
                var tb = document.getElementById("mytable"); //获取表格的dom节点

                for (var column = 0; column < column_table; column++) {
                    var td = tb.rows[row + 1].cells[column]; //获取0行0列的td单元格
                    //td.setAttribute('class', 'black');       //设置属性

                    if (column == 0) {
                        var code = data.list[row][column]
                        var name = data.list[row][column + 1]
                            // < !--td.innerHTML = "<div class=\"tb1 name\">" + String(name) + "</div>" + "<div class=\"code\">" + String(code) + "</div>"-- >
                        td.innerHTML = name
                        if (data.list[row][24]) {
                            td.setAttribute('class', 'green'); //设置属性
                        }
                    } else if (column == 1) {
                        var date = new Date(parseInt(data.list[row][6]) * 1000)
                        td.innerHTML = format_date(date);
                    } else if (column == 2) {
                        td.innerHTML = data.list[row][9];
                        if (data.list[row][9] != "首板") {
                            td.setAttribute('class', 'red'); //设置属性
                        }
                    } else if (column == 3) {
                        td.innerHTML = data.list[row][16];
                        //数据统计
                        all_theme = all_theme.concat({
                            name: data.list[row][16],
                            value: 1
                        })
                    } else if (column == 4) {
                        td.innerHTML = data.list[row][11];
                    } else if (column == 5) {
                        var date = new Date(parseInt(data.list[row][25]) * 1000)
                        td.innerHTML = format_date(date);
                    } else if (column == 6) {
                        td.innerHTML = format_money(data.list[row][23]);
                    } else if (column == 7) {
                        td.innerHTML = format_money(data.list[row][8]);
                    } else if (column == 8) {
                        td.innerHTML = format_money(data.list[row][12]);
                    } else if (column == 9) {
                        td.innerHTML = format_money(data.list[row][13]);
                    } else if (column == 10) {
                        td.innerHTML = (data.list[row][14] + "%");
                    } else if (column == 11) {
                        td.innerHTML = format_money(data.list[row][15]);
                    } else {
                        td.innerHTML = data.list[row][column]
                    }
                }

            }

        }

        function format_money(money) {
            //金钱转换
            if (money > 0) {
                if (money / 10000 > 10000) {
                    return (money / 100000000).toFixed(2) + "亿"
                } else if (money / 10000 > 1) {
                    return (money / 10000).toFixed(0) + "万"
                }
            } else {
                money = Math.abs(money);
                if (money / 10000 > 10000) {
                    return "-" + (money / 100000000).toFixed(2) + "亿"
                } else if (money / 10000 > 1) {
                    return "-" + (money / 10000).toFixed(0) + "万"
                }
            }
        }

        function format_date(data) {
            //格式化日期 (data=时间戳)
            new Date(parseInt(data) * 1000)
            var h = addZero(data.getHours());
            var m = addZero(data.getMinutes());
            var s = addZero(data.getSeconds());
            return h + ":" + m + ":" + s;

            function addZero(i) {
                if (i < 10) {
                    i = "0" + i;
                }
                return i;
            }
        }

        function add_row(row, column) {
            //增加新行
            var c = document.getElementById('mytable'); //获得表格的信息
            for (var i_row = 0; i_row < row + 1; i_row++) {
                var x = c.insertRow(1); //插入一行
                //设置 tr class
                if (i_row % 2 != 0) {
                    x.className = "cur"
                }
                for (var i = 0; i < column; i++) { //依次向新行插入表格列数的单元格
                    var y = x.insertCell(i);
                    // y.setAttribute('class', 'tb' + parseInt(i + 1));       //设置属性
                }
            }
        }

        function update_tables(data) {
            //增删表格
            var row = data['list'].length; //数据行数
            var c = document.getElementById('mytable'); //获得表格的信息
            var count_delete = c.rows.length - 1 - row
            if (count_delete != 0) { //如果是向一个空表增加一行
                if (count_delete > 0) {
                    for (var i = 0; i < count_delete; i++) {
                        c.deleteRow(1); //删除多余的行
                    }
                } else {
                    add_row(Math.abs(count_delete), column_table); //增加的行数
                }
            }
        }

        function count_all() {
            //数据统计
            var fruitTotal = []; // 存最终数据结果

            // 数据按照水果名称进行归类
            var nameContainer = {}; // 针对键name进行归类的容器
            all_theme.forEach(item => {
                nameContainer[item.name] = nameContainer[item.name] || [];
                nameContainer[item.name].push(item);
            });

            console.log(nameContainer); // 按照水果名称归类完成：{ apple: Array(2), banana: Array(2) }

            // 统计不同种类水果的数量
            var fruitName = Object.keys(nameContainer); // 获取水果种类：["apple", "banana"]
            fruitName.forEach(nameItem => {
                let count = 0;
                nameContainer[nameItem].forEach(item => {
                    count += item.value; // 遍历每种水果中包含的条目计算总数
                });
                fruitTotal.push({
                    value: count,
                    name: nameItem
                });
            });

            console.log(fruitTotal);
            return fruitTotal
        }

        function getData() {
            var m_data = []
            $.ajax({
                url: '/test',
                data: {},
                type: 'POST',
                async: false,
                dataType: 'json',
                success: function(data) {
                    UpdateTable(data);
                    // auto_update(data);
                    set_option();
                    set_title_zt(data);
                    m_data = data;
                },
                error: function(msg) {
                    console.log(msg);
                }
            })
            return m_data;
        };

        function get_data(date) {
            $.ajax({
                url: '/history',
                data: {
                    date
                },
                type: 'POST',
                async: false,
                dataType: 'json',
                success: function(data) {
                    history_update_table(data);
                    // auto_update(data);
                    set_option();
                    set_title_zt(data);
                },
                error: function(msg) {
                    console.log(msg);
                }
            })
        };

        function get_date() {
            //获取当前日期
            var d = new Date();
            var m_date = d.getFullYear() + "-" + formatZero(String(parseInt(d.getMonth()) + 1)) + "-" + formatZero(d.getDate());
            return m_date;
        }

        //格式化日期时间
        function formatZero(n) {
            if (n >= 0 && n <= 9) {
                return "0" + n;
            } else {
                return n;
            }
        }

        function get_time() {
            //获取当前时间
            var d = new Date();
            var m_date = d.getHours() + "-" + d.getMinutes() + "-" + d.getSeconds();
            // return m_date;
            // 判断时间是否在开盘时间段
            if ((15 * 60 * 60) > (d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds()) && (d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds()) > (9 * 60 * 60 + 15 * 60)) {
                return true
            } else {
                return false
            }
        }

        function is_running() {
            if (get_time()) {
                getData();
            } else {
                if (ID_THREAD != -1) {
                    clearInterval(ID_THREAD)
                }
            }
        }

        function auto_update() {
            data = getData();
            if (get_date() == data.day) {
                ID_THREAD = setInterval('is_running()', 1000 * 3); //指定3秒刷新一次
            } else {
                if (ID_THREAD != -1) {
                    clearInterval(ID_THREAD)
                }
            }
        }

        function set_title_zt(data) {
            //更新涨停数量
            var zt = document.getElementById('zt'); //获得表格的信息
            zt.innerHTML = "涨停板" + "(" + data['list'].length + ")"
        }
    </script>
    <script type="text/javascript">
        var dom = document.getElementById("container");
        var myChart = echarts.init(dom);
        var app = {};

        var option;

        function set_option() {
            option = {
                tooltip: {
                    trigger: 'item'
                },
                series: [{
                    name: 'Access From',
                    type: 'pie',
                    radius: '100%',
                    data: count_all(),
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }]
            };

            if (option && typeof option === 'object') {
                myChart.setOption(option);
            }
        }
        // < !--setInterval('set_option()', 1000 * 3); //指定1秒刷新一次-->
    </script>
</body>

</html>